<?php

// +----------------------------------------------------------------------
// | 权限组控制器
// +----------------------------------------------------------------------
// | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 微尘 <yicmf@qq.com>
// +----------------------------------------------------------------------

namespace app\controller\pay\admin;

use app\model\pos\Pos as PosModel;
use app\model\ucenter\User as UserModel;
use yicmf\builder\Edit;
use yicmf\builder\Table;
use app\controller\admin\Common;
use app\event\file\Attachment as AttachmentEvent;
use app\model\ucenter\UserMoneyLog as UserMoneyLogModel;
use app\model\pay\PointOrder as PointOrderModel;
use app\event\pay\PointOrder as PointOrderEvent;

class PointOrder extends Common
{
    protected function initialize()
    {
        parent::initialize();
    }

    public function _filter()
    {
        $where[] = ['status', '>', -2];
        return $where;
    }

    public function index()
    {

        $statusOptions = [
            [
                'id' => 0,
                'value' => '申请'
            ],
            [
                'id' => 1,
                'value' => '进行中'
            ],
            [
                'id' => 2,
                'value' => '已完成'
            ],
            [
                'id' => 3,
                'value' => '失败'
            ]
        ];
        $where = $this->_filter();

        //ID 姓名 手机号 等级  提现金额 手续费 提现费 实际入账  审核状态 驳回原因 申请时间  审核时间 事项 时间 资金类型
        // 显示页面
        $builder = new Table();
        $builder->model(PointOrderModel::class)
            ->where($where)
//                ->suggest('高等级可以对低等级用户进行操作或者授权')
            ->searchSelect('step', '进度', $statusOptions)
//            ->searchText('sn_start', '机身开始号')
//            ->searchText('sn_end', '机身结束号')
//            ->searchText('result_count', '显示总数')
//            ->keyLeftLeader('checkbox')
            ->keyId()
            ->keyText('name', '银行名称')
//            ->keyText('chaxun', '查询方法')
            ->keyText('point', '使用积分')
            ->keyText('price', '价值')
//            ->keyClosure('下单手机号', function ($data) {
//                return $data['user']['mobile'];
//            })
//            ->keyMap('flag', '类别',['银行','运营商'])
//            ->keyImage('poster_image', '海报')
            ->keyMap('step', '排序', ['申请', '进行中', '已完成', '失败'])//0申请，1进行中2已完成3失败
            ->keyText('user.mobile', '下单手机号')
            ->keyUser('user_id', '申请人')
            ->keyUpdateTime()
            ->actionUpdate('update?id={$id}', '跟进', [1])
            ->actionDelete();
        return $builder->fetch();
    }


    public function update()
    {
        if ($this->request->isPost()) {
            $event = new PointOrderEvent();
            $data = $event->update($this->request->param(), $this->user);
            if (0 === $data['code']) {
                $this->_returnSuccess($data['message']);
            } else {
                $this->_returnError($data['message'], false);
            }
        } else {
            // 同步节点与菜单
            if ($this->request->param('id')) {
                $group_id = $this->request->param('id');
                $data = PointOrderModel::where('id', $group_id)->find();
                if (!$data) {
                    $this->_returnError('记录不存在');
                }
            } else {
                $this->_returnError('记录不存在');
            }
            $data['status'] = 1;
            $builder = new Edit();
            $builder->title('兑换审核')
                ->keyLabel('name', '名称')
                ->keyLabel('chaxun', '查询方法')
                ->keyLabel('user.nickname', '申请人昵称')
                ->keyLabel('user.username', '申请人手机号')
//                ->keyDecimal('price', '价值','（每万积分）')
//                ->keyRadio('flag', '类别',['银行','运营商'])
                ->keyRadio('step', '进度', ['申请', '进行中', '已完成', '失败'])//0申请，1进行中2已完成3失败
                ->keyNumber('point', '使用积分')
                ->keyNumber('price', '核算价值')
                ->keyNumber('free_balance', '本金返现','免手续费')
                ->data($data)
                ->buttonSubmit();
            return $builder->fetch();
        }
    }

    /**
     * 删除信息
     * 改变status信息删除.
     */
    public function delete()
    {
        $event = new PointOrderEvent();
        $result = $event->delete($this->request->get(), $this->user);
        if (0 == $result['code']) {
            $this->_returnSuccess('删除成功');
        } else {
            $this->_returnError('失败，失败信息：' . $result['message']);
        }
    }


}
